package com.project.lx.text.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.project.lx.text.R;
import com.project.lx.text.base.BaseActivity;
import com.project.lx.text.base.BaseFragment;
import com.project.lx.text.contract.IMainContract;
import com.project.lx.text.fragment.FragmentA;
import com.project.lx.text.fragment.FragmentB;
import com.project.lx.text.fragment.FragmentC;
import com.project.lx.text.presenter.ActMainPresenterImpl;
import com.project.lx.text.widget.NotSlipViewPager;
import com.project.lx.text.widget.SlidingMenu;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity implements IMainContract.IMainView {

    @BindView(R.id.menu_01)
    TextView menu01;
    @BindView(R.id.menu_02)
    TextView menu02;
    @BindView(R.id.menu_03)
    TextView menu03;
    @BindView(R.id.menu_04)
    TextView menu04;
    @BindView(R.id.menu_05)
    TextView menu05;
    @BindView(R.id.menu_06)
    TextView menu06;
    @BindView(R.id.menu_07)
    TextView menu07;
    @BindView(R.id.menu_08)
    TextView menu08;
    @BindView(R.id.menu_09)
    TextView menu09;
    @BindView(R.id.no_scroll_viewPager)
    NotSlipViewPager noScrollViewPager;
    @BindView(R.id.btn_01)
    Button btn01;
    @BindView(R.id.btn_02)
    Button btn02;
    @BindView(R.id.btn_03)
    Button btn03;
    @BindView(R.id.sliding_menu)
    SlidingMenu slidingMenu;
    @BindView(R.id.act_main)
    LinearLayout actMain;

    //定义一个btn数组
    private Button btns[];
    private int btnID[];

    //定义一个Item用于设置显示页面
    private int item;

    //声明一个Presenter 对象，用于相关逻辑的处理
    private IMainContract.IMainPresenter presenter;
    //定义FragmentManager对象
    private FragmentManager manager;

    private BaseFragment mBaseFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initRootView() {
        setContentView(R.layout.activity_main);
    }

    @Override
    public void initView() {

        presenter = new ActMainPresenterImpl(this);

        //初始化按钮ID
        btnID = new int[]{R.id.btn_01, R.id.btn_02, R.id.btn_03};
        btns = new Button[]{btn01, btn02, btn03};

        //默认第一个按钮被选中
        btn01.setEnabled(false);
    }

    @Override
    public void initData() {
        //进行数据的初始化操作
        presenter.initData();
    }

    @Override
    public void initListener() {

    }

    @OnClick({R.id.menu_01, R.id.menu_02, R.id.menu_03, R.id.menu_04, R.id.menu_05, R.id.menu_06, R.id.menu_07, R.id.menu_08, R.id.menu_09, R.id.btn_01, R.id.btn_02, R.id.btn_03})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.menu_01:
                click(0, 0);
                break;
            case R.id.menu_02:
                click(0, 1);
                break;
            case R.id.menu_03:
                click(0, 2);
                break;
            case R.id.menu_04:
                click(1, 0);
                break;
            case R.id.menu_05:
                click(1, 1);
                break;
            case R.id.menu_06:
                click(1, 2);
                break;
            case R.id.menu_07:
                click(1, 3);
                break;
            case R.id.menu_08:
                click(2, 0);
                break;
            case R.id.menu_09:
                click(2, 1);
                break;
            case R.id.btn_01:
                changBtnSelectedStatus(0);
                noScrollViewPager.setCurrentItem(0, false);
                break;
            case R.id.btn_02:
                changBtnSelectedStatus(1);
                noScrollViewPager.setCurrentItem(1, false);
                break;
            case R.id.btn_03:
                changBtnSelectedStatus(2);
                noScrollViewPager.setCurrentItem(2, false);
                break;
        }
    }

    public void click(int f1, int f2) {
        changBtnSelectedStatus(f1);
        noScrollViewPager.setCurrentItem(f1);
        slidingMenu.toggleMenu();
        if (f1 == 0){
            FragmentA fragment = (FragmentA) getSupportFragmentManager().findFragmentByTag(
                    "android:switcher:" + R.id.no_scroll_viewPager + ":" + f1);
            if (fragment != null)  // could be null if not instantiated yet
            {
                if (fragment.getView() != null) {
                    fragment.mViewPager.setCurrentItem(f2);
                }
            }
        }else if (f1 == 1){
            FragmentB fragment = (FragmentB) getSupportFragmentManager().findFragmentByTag(
                    "android:switcher:" + R.id.no_scroll_viewPager + ":" + f1);
            if (fragment != null)  // could be null if not instantiated yet
            {
                if (fragment.getView() != null) {
                    fragment.mViewPager.setCurrentItem(f2);
                }
            }
        }else if (f1 == 2){
            FragmentC fragment = (FragmentC) getSupportFragmentManager().findFragmentByTag(
                    "android:switcher:" + R.id.no_scroll_viewPager + ":" + f1);
            if (fragment != null)  // could be null if not instantiated yet
            {
                if (fragment.getView() != null) {
                    fragment.mViewPager.setCurrentItem(f2);
                }
            }
        }

    }


    @Override
    public ViewPager getmActMainVpContent() {
        return noScrollViewPager;
    }

    @Override
    public FragmentManager getManager() {
        manager = getSupportFragmentManager();
        return manager;
    }

    @Override
    public void setPresenter(IMainContract.IMainPresenter presenter) {
        this.presenter = presenter;
    }

    /**
     * 按钮被选中的颜色变化
     *
     * @param position
     */
    public void changBtnSelectedStatus(int position) {
        for (int i = 0; i < 3; i++) {
            if (i == position) {
                //将选中的设置背景颜色为其他色
                btns[i].setEnabled(false);
            } else {
                //将为选中的设置背景颜色为默认
                btns[i].setEnabled(true);
            }
        }
    }
}
